package com.example.studentalarm.widget.head;

import android.animation.Animator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.studentalarm.R;
import com.example.studentalarm.utils.SPUtil;
import com.example.studentalarm.utils.StringUtil;
import com.example.studentalarm.utils.TimeUtils;


/**
 * Created by Administrator on 2017/6/22.
 * <p>
 * 苹果头部效果
 */
public class IPhoneHeader implements DragHeaderImpl {
    private Context mContext;
    private final int ROTATE_ANIM_DURATION = 180;
    private RotateAnimation mRotateUpAnim;
    private RotateAnimation mRotateDownAnim;
    //下拉刷新相关布局
    private LinearLayout ll_ok;
    private LinearLayout ll_refresh;
    private ImageView iv_refresh, iv_ok;
    private TextView tv_tip, tv_time, tv_ok;
    private ProgressBar pb_refresh;
    private int rotationSrc, arrowSrc,pullFailure;

    public IPhoneHeader(Context context) {
        this(context, R.mipmap.pull_up, R.mipmap.pull_ok,R.mipmap.pull_failure);
    }

    public IPhoneHeader(Context context, int rotationSrc, int arrowSrc, int pullFailure) {
        this.mContext = context;
        this.rotationSrc = rotationSrc;
        this.arrowSrc = arrowSrc;
        this.pullFailure=pullFailure;
        mRotateUpAnim = new RotateAnimation(0.0f, -180.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateUpAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateUpAnim.setFillAfter(true);
        mRotateDownAnim = new RotateAnimation(-180.0f, 0.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateDownAnim.setDuration(ROTATE_ANIM_DURATION);
        mRotateDownAnim.setFillAfter(true);
    }


    @Override
    public View getView(LayoutInflater inflater, ViewGroup viewGroup) {
        View refreshView = inflater.inflate(R.layout.layout_apple_refresh_header, viewGroup, false);
        ll_ok = refreshView.findViewById(R.id.ll_ok);
        ll_refresh = refreshView.findViewById(R.id.ll_refresh);
        iv_refresh = refreshView.findViewById(R.id.iv_refresh);
        iv_ok = refreshView.findViewById(R.id.iv_ok);
        tv_tip =  refreshView.findViewById(R.id.tv_tip);
        tv_time =  refreshView.findViewById(R.id.tv_time);
        tv_ok =  refreshView.findViewById(R.id.tv_ok);
        pb_refresh =  refreshView.findViewById(R.id.pb_refresh);
        getRefreshTime();
        return refreshView;
    }

    @Override
    public View getHeadView(LayoutInflater inflater, ViewGroup viewGroup, Animator.AnimatorListener listener) {
        return null;
    }

    @Override
    public void onPreDrag(View rootView) {
//        String time = SPUtil.getInstance(mContext).getRefreshTime("refreshTime");
//        if(!StringUtil.isCollectionEmpty(time)){
//           long freshTime=Long.parseLong(time);
//            int m = (int) ((System.currentTimeMillis() - freshTime) / 1000 / 60);
//            if (m >= 1 && m < 60) {
//                tv_time.setText("上次刷新:" +m + "分钟前");
//            } else if (m >= 60) {
//                int h = m / 60;
//                tv_time.setText("上次刷新:" +h + "小时前");
//            } else if (m > 60 * 24) {
//                int d = m / (60 * 24);
//                tv_time.setText("上次刷新:" +d + "天前");
//            } else if (m == 0) {
//                tv_time.setText("上次刷新:" +"刚刚");
//            }
//        }else{
//            tv_time.setText("当前刷新：" + TimeUtils.getTime(TimeUtils.TIME_FORMAT, System.currentTimeMillis()));
//        }
    }

    @Override
    public void onDropAnim(View rootView, int dy, int totalMoveDy) {

    }


    @Override
    public void onLimitDes(View rootView, boolean upOrDown) {
        if (!upOrDown) {
            ll_refresh.setVisibility(View.VISIBLE);
            ll_ok.setVisibility(View.GONE);
            tv_tip.setText("松开刷新");
            getRefreshTime();
            iv_refresh.setImageDrawable(mContext.getResources().getDrawable(rotationSrc));
            iv_refresh.clearAnimation();
            iv_refresh.startAnimation(mRotateUpAnim);
            pb_refresh.setVisibility(View.GONE);
            iv_refresh.setVisibility(View.VISIBLE);
        } else {
            ll_refresh.setVisibility(View.VISIBLE);
            ll_ok.setVisibility(View.GONE);
            tv_tip.setText("下拉刷新");
             getRefreshTime();
            iv_refresh.clearAnimation();
            iv_refresh.startAnimation(mRotateDownAnim);
            pb_refresh.setVisibility(View.GONE);
            iv_refresh.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onStartAnim() {
        ll_refresh.setVisibility(View.VISIBLE);
        ll_ok.setVisibility(View.GONE);
        tv_tip.setText("正在刷新...");
        getRefreshTime();
        SPUtil.getInstance(mContext).setRefreshTime("refreshTime", "" +
                TimeUtils.getDate(TimeUtils.TIME_FORMAT, System.currentTimeMillis()));//
        iv_refresh.clearAnimation();
        iv_refresh.setVisibility(View.GONE);
        pb_refresh.setVisibility(View.VISIBLE);
    }

    @Override
    public void onFinishAnim(boolean refreshOk) {
        ll_refresh.setVisibility(View.GONE);
        ll_ok.setVisibility(View.VISIBLE);
        tv_ok.setText(refreshOk?"刷新成功":"刷新失败");
        iv_ok.setImageDrawable(mContext.getResources().getDrawable(refreshOk?arrowSrc:pullFailure));
    }


    private void getRefreshTime() {
        String time = SPUtil.getInstance(mContext).getRefreshTime("refreshTime");
        if (StringUtil.isEmpty(time)) {
            tv_time.setText(new StringBuilder("当前刷新：").append(TimeUtils.getDate(TimeUtils.TIME_FORMAT, System.currentTimeMillis())).toString());
        } else {
            tv_time.setText(new StringBuilder("上次刷新:").append(time).toString());
        }
    }
}
